package com.knife.file.domain.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.knife.file.domain.entity.KnifeFileInfo;
import com.knife.file.domain.entity.KnifeStorageConfig;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;

/**
 * 文件上传抽象模板类
 *
 * @author： 76875
 * @date： 2022/4/6 星期三 16:54
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
public abstract class KnifeFileService {
    protected KnifeStorageConfig config;

    public KnifeFileService init(KnifeStorageConfig config) {
        this.config = config;
        return this;
    }

    /**
     * 通过本地文件上传
     *
     * @param file     文件信息
     * @param filePath 当前待上传文件路径
     * @return 文件url
     */
    public abstract String upload(KnifeFileInfo file, String filePath);

    /**
     * 通过输入流上传文件
     *
     * @param file        文件信息
     * @param inputStream 文件输入流
     * @return 文件url
     */
    public abstract String upload(KnifeFileInfo file, InputStream inputStream);


    /**
     * 拷贝文件
     *
     * @param file          文件信息
     * @param oldFileKey    原文件key
     * @param oldBucketName 原始桶名
     * @return
     */
    public abstract String copyFile(KnifeFileInfo file, String oldFileKey, String oldBucketName);

    /**
     * 删除文件
     *
     * @param bucketName 桶名
     * @param url        附件url
     * @param fileKey    附件唯一标识
     */
    public abstract void deleteFile(String bucketName, String url, String fileKey);

    /**
     * @param servletRequest
     * @param bucketName
     * @param url
     * @param fileKey
     * @param fileName
     * @param download
     * @param expires
     * @return
     */
    public abstract String getSignedUrl(HttpServletRequest servletRequest, String bucketName, String url, String fileKey, String fileName, boolean download, Long expires);

    /**
     * 下载文件
     *
     * @param request
     * @param response
     * @param bucketName
     * @param url
     * @param fileKey
     */
    public abstract void download(HttpServletRequest request, HttpServletResponse response, String bucketName, String url, String fileKey);

    public abstract void decryptDownload(HttpServletRequest request, HttpServletResponse response, String bucketName, String url, String fileKey, String password);

    /**
     * 获取完整前缀
     *
     * @param realBucketName
     * @return
     */
    public abstract String getObjectPrefixUrl(String realBucketName);


    protected void buildResponse(HttpServletResponse response, byte[] data, String fileName) throws IOException {
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.setContentType("multipart/form-data");
        response.addHeader("Content-Length", "" + data.length);
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setDateHeader("Expires", System.currentTimeMillis() + 1000L);
        IOUtils.write(data, response.getOutputStream());
    }

    protected String getRealBucketName(String bucketName) {
        return StringUtils.isNotBlank(config.getBucketPrefix()) ? String.format("%s-%s", config.getBucketPrefix(), bucketName) : bucketName;
    }

    protected String getFileKey(String bucketName, String url) {
        String prefixUrl = this.getObjectPrefixUrl(bucketName);

        try {
            return URLDecoder.decode(url, "UTF-8").substring(prefixUrl.length());
        } catch (Exception var5) {
            return url.substring(prefixUrl.length());
        }
    }
}
